package com.zzjc.web.util;

import com.zzjc.core.exception.ScBizException;
import com.zzjc.util.UUIDUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


public class UploadUtil {

    private String fileRealName;
    private String fileName;
    private String localFullPath;
    private String dirPath;
    private String filePath;
    private String urlPath;
    private String uploadPath;
    private String ext;

    public static UploadUtil build() {
        return new UploadUtil();
    }

    /**
     * 文件上传
     *
     * @return
     **/
    public UploadUtil uploadFile(MultipartFile multipartFile, String uploadPath) throws ScBizException {
        this.uploadPath = uploadPath;
        this.fileRealName = multipartFile.getOriginalFilename();
        System.out.println(fileRealName);
        //生成存放目录
        this.createDir();
        //生成本地文件
        this.createFile(multipartFile);
        return this;
    }

    private void createDir() {
        File file = new File(this.uploadPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String dateTime = DateUtils.dateTimeNow(DateUtils.YYYYMMDD);
        String dirPath = this.uploadPath + File.separator + dateTime;
        this.dirPath = dirPath;
        this.urlPath = dateTime;
        file = new File(dirPath);
        if (!file.exists()) {
            file.mkdir();
        }
        this.localFullPath = dirPath;
    }

    private void createFile(MultipartFile multipartFile) {
        String uuid = UUIDUtils.getUUID();
        String fileNamePre = DateUtils.dateTimeNow(DateUtils.YYYYMMDD);
        String ext = this.fileRealName.substring(fileRealName.lastIndexOf(".") + 1);
        this.fileName = fileNamePre + uuid.substring(0, 16) + "." + ext;
        this.urlPath = this.urlPath + "/" + this.getFileName();
        this.filePath = this.localFullPath + File.separator + this.fileName;
        this.ext = ext;
        File newFile = new File(this.filePath);
        try {
            multipartFile.transferTo(newFile);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            throw new ScBizException("文件类型异常");
        }
    }

    public String getFileRealName() {
        return fileRealName;
    }

    public void setFileRealName(String fileRealName) {
        this.fileRealName = fileRealName;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getLocalFullPath() {
        return localFullPath;
    }

    public void setLocalFullPath(String localFullPath) {
        this.localFullPath = localFullPath;
    }

    public String getDirPath() {
        return dirPath;
    }

    public void setDirPath(String dirPath) {
        this.dirPath = dirPath;
    }

    public String getUrlPath() {
        return urlPath;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public void setUrlPath(String urlPath) {
        this.urlPath = urlPath;
    }

    public String getUploadPath() {
        return uploadPath;
    }

    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }

    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }
}
